﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CollBrowser
{
    public partial class SplitPages : DevComponents.DotNetBar.Office2007Form
    {
        WebBrowser webBrowsern = new WebBrowser();


        private WebBrowser wb = null;

        /// <summary>
        /// 浏览器网址
        /// </summary>
        public WebBrowser Wb
        {
            get { return wb; }
            set { wb = value; }
        }

        private Form1 parent = null;
        /// <summary>
        /// 主页面
        /// </summary>
        public new Form1 ParentForm
        {
            get { return parent; }
            set { parent = value; }
        }

        public SplitPages()
        {
            InitializeComponent();
            Load += new EventHandler(SplitPages_Load);
        }

        void SplitPages_Load(object sender, EventArgs e)
        {
            Load -= new EventHandler(SplitPages_Load);
            //移除控件
            ParentForm.splitContainer1.Panel2.Controls.Remove(ParentForm.webBrowser1);
            //添加子控件
            splitContainer2.Panel2.Controls.Add(ParentForm.webBrowser1);
            FormClosed += new FormClosedEventHandler(SplitPages_FormClosed);
            wb = ParentForm.webBrowser1;
        }

        /// <summary>
        /// 关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SplitPages_FormClosed(object sender, FormClosedEventArgs e)
        {
            //移除控件
            splitContainer2.Panel2.Controls.Remove(ParentForm.webBrowser1);
            //添加子控件
            ParentForm.splitContainer1.Panel2.Controls.Add(ParentForm.webBrowser1);
        }

        /// <summary>
        /// 选择区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Click(object sender, EventArgs e)
        {
            ParentForm.buttonSelect_Click(sender, e);
        }

        /// <summary>
        /// 提取分页页面索引
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX8_Click(object sender, EventArgs e)
        {
            ParentForm.State = 当前操作功能项状态.提取分页索引;
            ((List<Uri>)ParentForm.record.索引列表).Clear();
            ((List<Uri>)ParentForm.record.页面).Clear();
            var currentEl = new ElementSearch(ParentForm.record.分页区域路径).Find(wb.Document);
            var allAElements = (from a in currentEl.GetElementsByTagName("a").Cast<HtmlElement>()
                                let href = ((mshtml.HTMLAnchorElement)(a.DomElement)).href
                                where href != null
                                select new Uri(href));
            ParentForm.record.索引列表 = allAElements.ToList();

            if (ParentForm.record.索引列表.Count() > 0)
            {
                //转到最后一页
                webBrowsern.Navigate(ParentForm.record.索引列表.Last());
                webBrowsern.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(webBrowsern_DocumentCompleted);
                webBrowsern.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowsern_DocumentCompleted);
            }
        }

        /// <summary>
        /// 加载完成时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void webBrowsern_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser wb = (sender as WebBrowser);


            //如果没有找到本地
            if (wb.IsOffline)
            {
                //返回出错
                return;
            }


            ///如果已经包含预览几直接返回
            if (wb.Document.Body.OuterHtml.Contains("CollBrowserCE9D275C-13D8-4486-AFAD-F4AA151BA93D"))
            {
                return;
            }
            

            #region 内容页面  ((List<Uri>)ParentForm.record.页面).Clear();
                                    ///当前选择的区域原素
            HtmlElement currentElpages = null;
            try
            {
                currentElpages = new ElementSearch(ParentForm.record.分页页面内容页区域).Find(wb.Document);
            }
            catch
            {
                // "出错..";
                return;
            }
            if (currentElpages == null)
            {
                return;
            }


            var allAElementspages = (from a in currentElpages.GetElementsByTagName("a").Cast<HtmlElement>()
                                let href = ((mshtml.HTMLAnchorElement)(a.DomElement)).href
                                where href != null
                                select new Uri(href));
            var listpages = ((List<Uri>)ParentForm.record.页面);

            if (allAElementspages.Count() > 0)
            {
 
                foreach (var item in allAElementspages)
                {
                    if (!listpages.Contains(item))
                    {
                        ((List<Uri>)ParentForm.record.页面).Add(item);
                    }

                }

                label1.Text = "当前内容页面" + listpages.Count;
            }
            #endregion

            #region 索引
            ///当前选择的区域原素
            HtmlElement currentEl = null;
            try
            {
                currentEl = new ElementSearch(ParentForm.record.分页区域路径).Find(wb.Document);
            }
            catch
            {
                // "出错..";
                return;
            }
            if (currentEl == null)
            {
                return;
            }


            var allAElements = (from a in currentEl.GetElementsByTagName("a").Cast<HtmlElement>()
                                let href = ((mshtml.HTMLAnchorElement)(a.DomElement)).href
                                where href != null
                                select new Uri(href));
            var list = ((List<Uri>)ParentForm.record.索引列表);

            if (allAElements.Count() > 0)
            {

                //是不是全部相同
                bool alltheSame = true;
                foreach (var item in allAElements)
                {
                    if (!list.Contains(item))
                    {
                        ((List<Uri>)ParentForm.record.索引列表).Add(item);
                        alltheSame = false;
                    }

                }
                if (!alltheSame)
                {

                    //转到最后一页
                    webBrowsern.Navigate(ParentForm.record.索引列表.Last());
                }

                label1.Text += "当前提取索引页码" + list.Count;
            }
            #endregion


        }

        /// <summary>
        /// 提取内容页面网址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX2_Click(object sender, EventArgs e)
        {

            ParentForm.record.内容列表 = ParentForm.record.页面;
        }

        /// <summary>
        /// 选取内容页面区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX3_Click(object sender, EventArgs e)
        {
            ParentForm.State = 当前操作功能项状态.分页页面内容页区域;
            ParentForm.buttonSelect_Click(sender, e);

        }



    }
}
